import _ from 'lodash';
import { Request, Response, NextFunction } from 'express';
import { getComment, createComment, deleteComment, getIdComment, getVideoUrlWai } from './videoPlay.service';
import axios from 'axios';
import { getTokenCookie } from '../tokenCookie/tokenCookie.service';
import { getMultisetIsVip } from '../house/house.service';
import { selectUserAuth } from '../user_auth/user_auth.service';

/**
 * 获取内容
 */
export const getVideoUrl = async (
  request: Request,
  response: Response,
  next: NextFunction
) => {
  let wx_vid = request.query.url
  let videData: any;

  const getToken_cookie = await getTokenCookie();
  const cookieData = getToken_cookie.data[0].cookie;
  const url = `https://mp.weixin.qq.com/cgi-bin/video?action=get_mp_video_play_url&vid=` + wx_vid + `&token=${getToken_cookie.data[0].token}`;
  const resp = await axios({
    method: "GET",
    url: url,
    headers: {
      withCredentials: true,
      "Access-Control-Allow-Credentials": true,
      'content-type': 'application/json',
      'Cookie': cookieData
    }
  }).then(async res => {
    videData = await res.data.url_info;
    let videos = await res.data.url_info;

    // if (videos && videos.length > 0) {
    //   let videoStrUrl = videos[0].url
    //   item.videoUrl = videoStrUrl.replace("mpvideo.qpic.cn", "wxsnsdy.wxs.qq.com/130/20210/snssvpdownload/SH/reserved", videoStrUrl)
    //   item.videoUrl = videos[0].url;
    // }
  }).catch(err => {
    console.error("err: ", err);
  })


  response.send(videData);
}

/**
 * 获取视频链接
 */
export const getReqVideoUrlWai = async (
  request: Request,
  response: Response,
  next: NextFunction
) => {
  let wxUrl = request.query.url;
  let postId = request.query.postId;
  let isPlay = false;

  if (request.user !== undefined) {
    // 用户 ID
    let user_id = request.user.id;

    // 检查是否有会员
    let isVipUser = await selectUserAuth(user_id);

    // 当前时间
    let theTime = parseInt(`${new Date().getTime()}`, 10)

    // 结束时间
    let endTime = 0;
    if (isVipUser[0] !== undefined && isVipUser[0].endTime) endTime = isVipUser[0].endTime;
    
    // 判断让不让播放
    if (endTime - theTime > 0) isPlay = true;
  }

  let data = await getVideoUrlWai(wxUrl as string);
  
  let multiSetUrl = wxUrl as any;
  if (multiSetUrl.startsWith('wxv')) {
    data = await getVideoUrlWai(wxUrl as string);
  } else {
    data = wxUrl;
  }
  
  const isVip = await getMultisetIsVip(parseInt(`${postId}`, 10));
  if (isVip[0].is_free == 0) isPlay = true;
  
  let item: any = {};
  item.videoUrl = data;
  item.isPlay = isPlay;

  // 提供数据
  response.send(item);
}

/**
 * 获取内容(按 ID 查找)
 */
export const commentDetail = async (
  request: Request,
  response: Response,
  next: NextFunction
) => {
  // 获取内容 ID
  const { postId } = request.params;

  // 调取内容
  try {
    const data = await getIdComment(parseInt(postId, 10));

    // 做出响应
    response.send(data);
  } catch (error) {
    next(error);
  }
};

/**
 * 创建内容
 */
export const store = async (
  request: Request,
  response: Response,
  next: NextFunction
) => {
  // 获取内容
  let commentModel = request.body;
  
  // 创建内容
  try {
    const data = await createComment(commentModel);

    response.send(data);
  } catch (error) {
    next(error);
  }
}


/**
 * 删除内容
 */
export const destroy = async (
  request: Request,
  response: Response,
  next: NextFunction
) => {
  // 获取删除内容 ID
  const { postId } = request.body;

  // 删除内容
  try {
    const data = await deleteComment(parseInt(postId, 10));

    response.send(data);
  } catch (error) {
    next(error);
  }
};


